package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.PostDto;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
public class RoleController {

    @Autowired
    private RoleService roleService;


    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation(value = "角色分页",notes = "角色分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "RoleDto"),
            @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码",example = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数",example = "10",dataType = "Integer")
    })
    public ResponseResult findByPage(@RequestBody RoleDto roleDto,
                                     @PathVariable("pageNum") int pageNum,
                                     @PathVariable("pageSize") int pageSize){

       PageResponse<RoleVo> pageResponse = roleService.findByPage(roleDto,pageNum,pageSize);

       return  ResponseResult.success(pageResponse);

    }



    @PatchMapping()
    @ApiOperation(value = "角色修改",notes = "角色修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "RoleDto")
    })
    public ResponseResult updateRole(@RequestBody RoleDto roleDto){

        Boolean success = roleService.updateRole(roleDto);

        return  ResponseResult.success(success);

    }


    @GetMapping("/find-checked-resources/{roleId}")
    @ApiOperation(value = "根据角色id回显菜单权限",notes = "根据角色id回显菜单权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId",value = "角色Id",required = true,dataType = "Long")
    })
    public ResponseResult findResourcesByRoleId(@PathVariable("roleId") Long roleId){

        Set<String> resourceIds = roleService.findResourcesByRoleId(roleId);

        return  ResponseResult.success(resourceIds);

    }

    @PostMapping("/init-roles")
    @ApiOperation(value = "展示所有角色",notes = "展示所有角色")
    public ResponseResult findAll(){

        List<RoleVo> voList = roleService.findAll();

        return  ResponseResult.success(voList);

    }
}
